let currentPage = 1;
let pages;
let params = {
    order: "cost_id",
    count: 3,
    page: 1
};

$(() => {

    // 默认排序方式
    $.get("/fee/showCost", params, showDetails);


// 查询数据拼接
    function showDetails(resp) {

        if (resp.message === 'OK') {
            let totalPage = resp.data.totalCount;
            pages = Math.ceil(totalPage / params.count);

            let tBody = $('#tbody');
            tBody.empty();

            for (let i of resp.data.items) {

                i.baseDuration = i.baseDuration == null ? '-' : i.baseDuration;
                i.baseCost = i.baseCost == null ? '-' : i.baseCost;
                i.unitCost = i.unitCost == null ? '-' : i.unitCost;
                i.creatime = i.creatime == null ? '-' : i.creatime;
                i.startime = i.startime == null ? '-' : i.startime;

                i.descr = i.descr == null ? '-' : i.descr;


                switch (i.costStatus) {
                    case 0:
                        i.costStatus = '暂停';
                        break;
                    case 1:
                        i.costStatus = '开通';
                        break;
                    case 2:
                        i.costStatus = '删除';
                        break;
                }

                let tr = $('<tr></tr>');
                let td01 = $('<td></td>').html(i.costId);

                let detail = $('<a href="/fee/fee_detail.html" class="btn_modify"></a>').html(i.costName);
                let td02 = $('<td></td>');
                td02.append(detail);

                let td03 = $('<td></td>').html(i.baseDuration);
                let td04 = $('<td></td>').html(i.baseCost);
                let td05 = $('<td></td>').html(i.unitCost);
                let td06 = $('<td></td>').html(i.creatime);
                let td07 = $('<td></td>').html(i.startime);
                let td08 = $('<td></td>').html(i.costStatus);
                let td09 = $('<td></td>');

                let btn_start;
                switch (i.costStatus) {
                    case '暂停':
                        btn_start = $('<input type="button" value="启用" class="btn_start" />');
                        break;
                    case '开通':
                        btn_start = $('<input type="button" value="暂停" class="btn_pause" />');
                        break;
                }

                let btn_modify = $('<input type="button" value="修改" class="btn_modify" />');


                let btn_delete = $('<input type="button" value="删除" class="btn_delete" />');
                td09.append(btn_start).append(btn_modify).append(btn_delete);

                let td10 = $('<td style="display: none"></td>').html(i.descr);
                let td11 = $('<td style="display: none"></td>').html(i.costType);


                tBody.append(tr);
                tr.append(td01).append(td02).append(td03)
                    .append(td04).append(td05).append(td06)
                    .append(td07).append(td08).append(td09)
                    .append(td10).append(td11);

            }

            // 调用画页码函数
            drawPagesCount(pages);

        }

    }

    $("body").on("click", ".btn_modify", function () {
        let costId = $(this).parents("tr").children().eq(0).text();
        let costName = $(this).parents("tr").children().eq(1).text();
        let baseDuration = $(this).parents("tr").children().eq(2).text();
        let baseCost = $(this).parents("tr").children().eq(3).text();
        let unitCost = $(this).parents("tr").children().eq(4).text();
        let creatime = $(this).parents("tr").children().eq(5).text();
        let startime = $(this).parents("tr").children().eq(6).text();
        let costStatus = $(this).parents("tr").children().eq(7).text();
        let costType = $(this).parents("tr").children().eq(10).text();
        let descr = $(this).parents("tr").children().eq(9).text();


        window.localStorage.setItem("costId", costId);
        window.localStorage.setItem("costName", costName);
        window.localStorage.setItem("baseDuration", baseDuration);
        window.localStorage.setItem("baseCost", baseCost);
        window.localStorage.setItem("unitCost", unitCost);
        window.localStorage.setItem("creatime", creatime);
        window.localStorage.setItem("startime", startime);
        window.localStorage.setItem("costStatus", costStatus);
        window.localStorage.setItem("costType", costType);
        window.localStorage.setItem("descr", descr);

        window.location.href = "/fee/fee_modi.html";

    });


    //排序按钮的点击事件
    let fee = $('#fee');
    let duration = $('#duration');
    fee.click(function () {

        if (fee.attr("class") === "sort_desc") {
            fee.attr("class", "sort_asc");
            params.order = "base_cost";
            $.get("/fee/showCost", params, showDetails);
        }
        else if (fee.attr("class") === "sort_asc") {
            fee.attr("class", "sort_desc");
            params.order = "base_cost DESC";
            $.get("/fee/showCost", params, showDetails);
        }
    });

    duration.click(function () {
        if (duration.attr("class") === "sort_desc") {
            duration.attr("class", "sort_asc");
            params.order = "base_duration";
            $.get("/fee/showCost", params, showDetails);

        } else if (duration.attr("class") === "sort_asc") {
            duration.attr("class", "sort_desc");
            params.order = "base_duration DESC";
            $.get("/fee/showCost", params, showDetails);
        }
    });

    // 画页码
    function drawPagesCount(pages) {
        let ul = $('#page');
        ul.empty();
        for (let i = 1; i <= pages; i++) {
            let li = $('<li style="float: left"></li>');
            let a = $('<a></a>');
            if (currentPage == i) {
                a.addClass('current_page');
            }
            a.attr('href', 'javascript:void(0)');
            a.text(i);
            a.click(function () {
                params.page = i;
                $.get("/fee/showCost", params, showDetails);
                currentPage = i;
            });
            li.append(a);
            ul.append(li);
        }
    }

    // 上一页
    $('#prev').click(function () {
        --currentPage;
        if (currentPage < 1) {
            currentPage = 1;
        }

        params.page = currentPage;
        $.get("/fee/showCost", params, showDetails);
    });

    // 下一页
    $('#next').click(function () {
        ++currentPage;
        if (currentPage > pages) {
            currentPage = pages;
        }
        params.page = currentPage;
        $.get("/fee/showCost", params, showDetails);
    });



    // 启动
    $("body").on("click", ".btn_start", function () {
        let costId = $(this).parents("tr").children().eq(0).text();
        let params = {
            costId: costId,
            costStatus: 1
        };
        let flag = window.confirm("确定要启用此资费吗？资费启用后将不能修改和删除。");
        if (flag) {
            $(this).attr("class", "btn_pause");
            $(this).val("暂停");
            $.post("/fee/costSwitch", params);
            window.location.reload();
        }

    });

    // 暂停
    $("body").on("click", ".btn_pause", function () {
        let costId = $(this).parents("tr").children().eq(0).text();
        let params = {
            costId: costId,
            costStatus: 0
        };
        let flag = window.confirm("确定要暂停此资费吗？");
        if (flag) {
            $(this).attr("class", "btn_start");
            $(this).val("启用");
            $.post("/fee/costSwitch", params);
            window.location.reload();
        }

    });

    // 删除
    $("body").on("click", ".btn_delete", function () {

        let costId = $(this).parents("tr").children().eq(0).text();
        let params = {
            costId: costId,
            costStatus: 2
        };
        let flag = window.confirm("确定要删除此资费吗？");
        if (flag) {
            $.post("/fee/costSwitch", params);
            window.location.reload();
        }

    });


});




